package com.bjgzy.rating.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bjgzy.rating.R;
import com.bjgzy.rating.R2;
import com.bjgzy.rating.di.component.DaggerSignUpComponent;
import com.bjgzy.rating.mvp.contract.SignUpContract;
import com.bjgzy.rating.mvp.presenter.SignUpPresenter;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.EntriesDetailData;
import com.eduhzy.ttw.commonsdk.entity.RatingHomeData;
import com.eduhzy.ttw.commonsdk.entity.SaveWorksData;
import com.eduhzy.ttw.commonsdk.entity.SignUp2Data;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-9 11:43:59
 * <p>
 * 活动评比报名信息第一页
 */
@Route(path = RouterHub.RATING_SIGNUP2ACTIVITY)
public class SignUp2Activity extends BaseActivity<SignUpPresenter> implements SignUpContract.View {
    @Inject
    AppManager mAppManager;
    @BindView(R2.id.tv_title)
    EditText tvTitle;
    @BindView(R2.id.tv_content)
    EditText tvContent;
    @BindView(R2.id.tv_depart)
    EditText tvDepart;
    @BindView(R2.id.tv_phone)
    EditText tvPhone;
    @BindView(R2.id.checkbox)
    CheckBox checkbox;
    private SaveWorksData saveData;
    @Autowired(name = Constants.ROUTER_DATA)
    RatingHomeData data;
    @Autowired(name = Constants.PARCELABLE_DATA)
    EntriesDetailData mEntriesDetailData;
    @Autowired(name = Constants.ROUTER_INTEGER)
    int cid;
    QMUITipDialog dialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        //如找不到该类,请编译一下项目
        DaggerSignUpComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.rating_activity_sign_up2;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        saveData = new SaveWorksData();
        if (ObjectUtils.isNotEmpty(mEntriesDetailData)) {
            tvDepart.setText(mEntriesDetailData.getUnionUnit());
            tvPhone.setText(mEntriesDetailData.getTelephone());
            checkbox.setChecked(mEntriesDetailData.getIsShare() == 1);
            tvTitle.setText(mEntriesDetailData.getWorksName());
        }

    }

    @Override
    public void showLoading() {
        if (dialog == null) {
            dialog = RxUtil.showLoading(getActivity(), "正在提交数据···");
        }
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(dialog)) {
            dialog.dismiss();
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void update(SignUp2Data signUp2Data) {
        if (ObjectUtils.isEmpty(signUp2Data)) {
            showMessage("操作失败");
            return;
        }
        Message msg = Message.obtain();
        msg.what = EventBusTags.WORKS_UPLOAD_SUCCESS;
        EventBus.getDefault().post(msg, RouterHub.RATING_RATINGDETAILFRAGMENT);
        EventBus.getDefault().post(msg, RouterHub.RATING_MYWORKSACTIVITY);
        if (saveData.getWorksId() > 0) {

            new QMUIDialog.MessageDialogBuilder(this)
                    .setTitle("修改成功")
                    .setMessage("是否继续修改作品文件")
                    .setCanceledOnTouchOutside(false)
                    .setCancelable(false)
                    .addAction("取消", (QMUIDialog dialog, int index) -> {
                        msg.what = EventBusTags.WORKS_UPLOAD_SUCCESS_WITHOUT_FILE;
                        EventBus.getDefault().post(msg, RouterHub.RATING_ENTRIESDETAILACTIVITY);
                        dialog.dismiss();
                        killMyself();
                    })
                    .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_POSITIVE,
                            (QMUIDialog dialog, int index) -> {
                                ARouter.getInstance()
                                        .build(RouterHub.RATING_SIGNUP3ACTIVITY)
                                        .withString(Intent.EXTRA_TITLE, Constants.SIGNUP)
                                        .withParcelable(Constants.ROUTER_DATA, signUp2Data)
                                        .withParcelable(Constants.PARCELABLE_DATA, data)
                                        .navigation();
                                dialog.dismiss();
                                killMyself();
                            })
                    .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();

        } else {
            ARouter.getInstance()
                    .build(RouterHub.RATING_SIGNUP3ACTIVITY)
                    .withString(Intent.EXTRA_TITLE, Constants.SIGNUP)
                    .withParcelable(Constants.ROUTER_DATA, signUp2Data)
                    .withParcelable(Constants.PARCELABLE_DATA, data)
                    .navigation();
            killMyself();
        }

    }

    @OnCheckedChanged(R2.id.checkbox)
    public void onCheckChanged(CompoundButton view, boolean checked) {
        saveData.setIsShred(checked ? 1 : 0);
    }

    @OnClick(R2.id.btn_next)
    public void onViewClicked() {
        if (RxUtil.showEditTip(this, tvTitle, tvTitle.getHint().toString())) return;
//        if (RxUtil.showEditTip(this, tvContent, tvContent.getHint().toString())) return;
        if (RxUtil.showEditTip(this, tvDepart, tvContent.getHint().toString())) return;
        if (!RxUtil.showEditTip(this, tvPhone, tvPhone.getHint().toString())) {
            saveData.setWorkName(tvTitle.getText().toString());
            saveData.setResearchId(data.getId());
            saveData.setUserId(SPUtils.getInstance().getString(Constants.USERID));
            saveData.setDanwei(tvDepart.getText().toString());
            saveData.setPhone(tvPhone.getText().toString());
            saveData.setcId(cid);
            if (data.getWorksId() > 0) {
                saveData.setWorksId(data.getWorksId());
            }
            MobclickAgent.onEvent(getActivity(),"ttw_rating_upload");
            mPresenter.saveWorks(saveData);
        }

    }

}
